// 流程审批 approval
import dicts from '@/mixins/dicts'
import { formatDate } from '@/mixins/filters'
import layer from '@/components/layer'
import permission from '@/components/permission'

export default {
    mixins: [dicts],
    data() {
        return {
            userInfo: this.$store.state.common.userInfo,
            tableData: {
                status: 0,    // 状态 0.提案审批、2.案件类型变更审批、6.结案审批
                filter: {},   // 筛选
                flag: false,
                total: 0,
                sizes: 10,    // 选择每页显示条目个数
                size: 10,
                index: 1,
                data: []
            },
            viewContentLayer: {
                show: false,
                title: '',
                content: '',
                btns: ['关闭']
            },
            // 角色列表
            roleList: [],
            // 审核人
            superiorUser: [],
            roleForm: {
                name: ''
            },
            targetForm: {
                id: 0
            },
            // 30005:不需要选择审核人
            targetActive: true
        }
    },
    components: {
        layer,
        permission
    },
    methods: {
        // 筛选
        sumbitFilter() {
            this.tableData.flag = false
            this.getTableData(1)
        },
        // 重置
        resetFilter() {
            this.tableData.filter = {}
            this.sumbitFilter()
        },
        // table每页条数改变
        tableSizeChange(val) {
            this.tableData.size = val
            this.tableData.flag = false
            this.getTableData(1)
        },
        // 获取table数据
        getTableData(index) {
            if (this.riskCategoryForm.id) {
                let _category = this.dicts.riskCategory.find(item => item.id == this.riskCategoryForm.id)
                if (_category) {
                    this.tableData.filter.riskCategory = _category.value
                }
            }
            else {
                this.tableData.filter.riskCategory = ''
            }
            if (this.riskTypeForm.value) {
                this.tableData.filter.riskType = this.riskTypeForm.value
            }
            else {
                this.tableData.filter.riskType = ''
            }
            if (this.riskLevelForm.value) {
                this.tableData.filter.riskLevel = this.riskLevelForm.value
            }
            else {
                this.tableData.filter.riskLevel = ''
            }
            let _data = this.tableData.filter
            this.tableData.index = index
            _data.pageBean = {}
            _data.pageBean.pageIndex = index
            _data.pageBean.pageSize = this.tableData.size
            let _apiUrl = ''
            if (this.tableData.status > 0) {
                _apiUrl = 'queryCaseExamine'
                _data.caseStatus = this.tableData.status
            }
            else {
                _apiUrl = 'queryClueExamine'
            }
            api[_apiUrl](_data).then(res => {
                if (!this.tableData.flag) {
                    this.tableData.total = res.totalCount
                    this.tableData.flag = true
                }
                this.tableData.data = []
                res.list.map(item => {
                    if (item.clueDate) {
                        item.clueDate = formatDate(item.clueDate)
                    }
                    if (item.threadDate) {
                        item.threadDate = formatDate(item.threadDate)
                    }
                    if (item.cdate) {
                        item.cdate = formatDate(item.cdate)
                    }
                    if (item.applyDate) {
                        item.applyDate = formatDate(item.applyDate)
                    }
                    if (item.gainCaseDate) {
                        item.gainCaseDate = formatDate(item.gainCaseDate)
                    }
                    if (item.source) {
                        let _obj = this.dicts.sourceType.find(o => o.value == item.source)
                        if (_obj) {
                            item.sourceName = _obj.name
                        }
                    }
                    if (item.product) {
                        let _obj = this.dicts.productType.find(o => o.value == item.product)
                        if (_obj) {
                            item.productName = _obj.name
                        }
                    }
                    if (item.riskCategory) {
                        let _obj = this.dicts.riskCategory.find(o => o.value == item.riskCategory)
                        if (_obj) {
                            item.riskCategoryName = _obj.name
                        }
                    }
                    if (item.riskType) {
                        let _obj = this.dicts.riskType.find(o => o.value == item.riskType)
                        if (_obj) {
                            item.riskTypeName = _obj.name
                        }
                    }
                    if (item.riskLevel) {
                        let _obj = this.dicts.riskLevel.find(o => o.value == item.riskLevel)
                        if (_obj) {
                            item.riskLevelName = _obj.name
                        }
                    }
                    if (item.caseType) {
                        let _obj = this.dicts.caseType.find(o => o.value == item.caseType)
                        if (_obj) {
                            item.caseTypeName = _obj.name
                        }
                    }
                    if (item.caseStatus) {
                        let _obj = this.dicts.caseStatus.find(o => o.value == item.caseStatus)
                        if (_obj) {
                            item.caseStatusName = _obj.name
                        }
                    }
                    if (item.clueStatus) {
                        let _obj = this.dicts.clueStatus.find(o => o.value == item.clueStatus)
                        if (_obj) {
                            item.clueStatusName = _obj.name
                        }
                    }
                    return item
                })
                this.tableData.data = res.list
            }).catch(res => {
                console.log(res)
            })
        },
        // 查看内容
        showViewContentLayer(title, content) {
            this.viewContentLayer.show = true
            this.viewContentLayer.title = title
            this.viewContentLayer.content = content
        },
        // 获取角色列表
        queryRoleList(type) {
            this.roleForm = {}
            this.roleList = []
            this.selectClearItem()
            api.queryCirculation({ type: type }).then((res) => {
                this.roleList = res
            }).catch(res => {
                if (res.data.code == '30005') {
                    this.targetActive = false
                }
            })
        },
        // 选中角色
        selectRole(id) {
            this.selectClearItem()
            api.queryRoleUserById({ id: id }).then(res => {
                this.superiorUser = res
            }).catch(res => {
                console.log(res)
            })
        },
        // 选中角色清空选项
        selectClearItem() {
            this.superiorUser = []
            this.targetForm = {}
            this.targetActive = true
        }
    }
};
